package com.eding.controller.user;

import com.eding.constant.StatusConstant;
import com.eding.entity.Dish;
import com.eding.result.Result;
import com.eding.service.DishService;
import com.eding.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")
@Slf4j
@Api(tags = "C端-菜品浏览接口")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 用户端根据分类id查询菜品和口味
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list(Long categoryId) {
        //构造存进redis中的数据的key
        String key = "dish_" + categoryId;
        //先从redis数据库中查询该分类的菜品，如果存在则直接返回
//        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
//        if (list != null){
//            return Result.success(list);
//        }

        //若不存在则查询mysql数据库
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品
        List<DishVO> list = dishService.listWithFlavor(dish);

        //查询出mysql数据库的数据存放到redis中
//        redisTemplate.opsForValue().set(key,list);

        return Result.success(list);
    }

}
